/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.mwas.proteana.math.numerical;

import java.math.BigDecimal;
import java.math.MathContext;

/**
 *
 * @author loskov
 */
public class HypergeometricTest {
    
    protected BigDecimal population;  //N
    protected BigDecimal sample;      //n
    protected BigDecimal popSuccess;  //k
    protected BigDecimal sampSuccess; //x
    protected BigDecimal result;
    
    public HypergeometricTest(int population, int sample, int popSuccess, int sampSuccess) {
        this.population = new BigDecimal(population);
        this.sample = new BigDecimal(sample);
        this.popSuccess = new BigDecimal(popSuccess);
        this.sampSuccess = new BigDecimal(sampSuccess);
        calculate();
    }

    public HypergeometricTest(BigDecimal population, BigDecimal sample, BigDecimal popSuccess, BigDecimal sampSuccess) {
        this.population = population;
        this.sample = sample;
        this.popSuccess = popSuccess;
        this.sampSuccess = sampSuccess;
        calculate();
    }
    
    private BigDecimal calculate() {
        BigDecimal c_kx = new Binomial(popSuccess, sampSuccess).getResult();
        BigDecimal c_Nk_nx = new Binomial(population.subtract(popSuccess), sample.subtract(sampSuccess)).getResult();
        BigDecimal c_Nn = new Binomial(population, sample).getResult();
        
        result = c_kx.multiply(c_Nk_nx).divide(c_Nn, MathContext.DECIMAL64);
        return result;
    }

    public BigDecimal getResult() {
        return result;
    }
    
}
